/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sad.controle;

/**
 *
 * @author Damiao
 */
import sad.util.Constantes;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.Vector;
import sad.dao.AvaliacaoDAO;
import sad.dao.AvaliacaoTableDAO;
import sad.dao.DAO;
import sad.dao.DisciplinaTableDAO;
import sad.dao.ProfessorTableDAO;
import sad.modelo.Professor;
import sad.util.DAOEvent;
import sad.visao.CadAvaliacaoDialog;
import sad.visao.DialogAbstrato;
import sad.visao.FabricaDeDialog;

public class AvaliacaoControle extends AbstractControl {

  private CadAvaliacaoDialog v_aval;

  public AvaliacaoControle(DialogAbstrato v) {
    this.v_aval = (CadAvaliacaoDialog)v;
    addOuvinte();
    initComponents();
  }
  
// <editor-fold defaultstate="collapsed" desc="Responde a eventos com origem nas visões">
    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getActionCommand().equals(Constantes.SALVAR_DISCIPLINA)){
            createAvaliacao();
        }else {
            if(e.getActionCommand().equals(Constantes.EXCLUIR)){
                int i = AvaliacaoTableDAO.getInstance().getIndexDisc();
                removeDisciplina(i);
            }
            if(e.getActionCommand().equals(Constantes.UPDATE)){
                int i = AvaliacaoTableDAO.getInstance().getIndexDisc();
                updateDisciplina(i);
            }
            if(e.getActionCommand().equals(Constantes.RELATORIO_QUESTAO)){
            FabricaDeDialog.criarDialog(v_aval.getParente(), Constantes.RELATORIO_QUESTAO, null);
        }
            AvaliacaoDAO.getInstance().removeDAOOuvinte(this);
            v_aval.close();
            
        }
    }

    @Override
    public void windowClosed(WindowEvent e){
        switch(e.getID()){
           case WindowEvent.WINDOW_CLOSING:
                AvaliacaoDAO.getInstance().removeDAOOuvinte(this);
                
                DAO.closeObjectContainer();
                v_aval.close();
                break;
        }
    }// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Enchaminha eventos com origem no DAO">
    @Override
    public void daoChange(DAOEvent e) {
           v_aval.modelDAOChange(e);
    }// </editor-fold>

    private void addOuvinte() {
        v_aval.addActionListener(this);
        DisciplinaTableDAO.getInstance().addDAOOuvinte(this);
    }

   

    private void createAvaliacao() {
          /*  String nome  = v_aval;
          //  String abrev = v_aval.getAbreviatura();
          //  String p = v_aval.getProfessor();
            
            if(verificarOK(nome, abrev, p, 0)) {
                Professor prof = ProfessorDAO.getProfessorSaved(p);
                Disciplina d=DisciplinaDAO.getInstance().createTableDisc(nome, abrev, prof);
                DisciplinaTableDAO.getInstance().addDisciplina(d);
            }  */
     }
        
    private void removeDisciplina(int i) {
       // DisciplinaTableDAO.getInstance().removeDisciplina(i);
    }

    private void updateDisciplina(int i) {
         //   String nome  = v_aval.getNome();
         /*   String abrev = v_aval.getAbreviatura();
            String p = v_aval.getProfessor();

            if(verificarOK(nome, abrev, p, 1)){
                Professor prof = ProfessorDAO.getProfessorSaved(p);
                Disciplina d=DisciplinaTableDAO.getInstance().getDiscAt(i);
                d.setNome(nome);
                d.setAbrev(abrev);
                d.setProf(prof);
                DisciplinaDAO.getInstance().update(d);
                DisciplinaTableDAO.getInstance().update(d, i);
            }*/
    }
    
    private boolean verificarOK(String nome, String abrev, String p,  int tipo) {
            boolean ok=false;
            if (nome.equals("")){
                DisciplinaTableDAO.getInstance().erroField(nome, Constantes.NOME_DISCIPLINA);
            }else {
                if (abrev.equals("")){
                    DisciplinaTableDAO.getInstance().erroField(abrev, Constantes.ABREV_DISCIPLINA);
                }else{
                      if (p.equals("<Lista vazia>")){
                           DisciplinaTableDAO.getInstance().erroField(p, Constantes.PROFESSOR);
                      }else{
                          if(tipo ==0){
                              Professor prof = ProfessorTableDAO.getProfessorSaved(p);
                              if(!DisciplinaTableDAO.getInstance().isExist(nome, abrev, prof)) {
                                ok=true;
                            }
                          }else{
                           ok=true;   
                          }
                    }  
                }
            }
            return ok;
    }

    private void initComponents() {
       Object[] listObjects = DAO.getListObjects(Professor.class);
       Vector<String> p = new Vector<String>();
       if (listObjects.length!=0){
           for (int i = 0; i < listObjects.length; i++) {
                Professor prof = (Professor) listObjects[i];
                p.add(prof.getNome());
           }
          // v_aval.reload(p);
       }
    }
}
